#coding:utf-8

import time
import temp_mes_fasong
import threading
import libs.util.redis_queue as redis_queue
import config
import json

def __process_message():

    sub = redis_queue.sub(config.muban_tunnel_name)
    '监听'
    for m in sub.listen():
        try:
            message = json.loads(m['data'])
            # obj = temp_mes_fasong[message['type'] + "_class"]()
            #
            # temp_mes_fasong.dic[message['type']] ()
            data_obj = temp_mes_fasong.dycg_class()
            data_obj.first = u'您有一条新消息'
            data_obj.keyword1 = message['title']
            data_obj.keyword2 = time.strftime("%Y-%m-%d")
            data_obj.remark = u"点我查看详细内容"
            touser = message['touser']
            url = message['url']
            temp_mes_fasong.dycg(touser,url,data_obj)
        except:
            pass
        time.sleep(5)


def send_message(message):
    redis_queue.pub(config.muban_tunnel_name, message)

invoke_time = [0]
#奇怪的问题，为什么会被调用两次
def start_listen():
    #python2的bug
    if(invoke_time[0] > 0):
        return
    invoke_time[0] += 1
    print("invoked")
    t = threading.Thread(target= __process_message)
    t.start()

# sub = redis_queue.sub("123")
# for m in sub.listen():
#     print m['data']
#
#
# redis_queue.pub("123","ddd")